package com.elink.enginerring.user;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.elink.enginerring.common.Constant;
import com.elink.enginerring.common.ErrorType;
import com.elink.enginerring.datasource.DataSourceContextHolder;
import com.elink.enginerring.datasource.DataSourceType;
import com.elink.enginerring.user.entity.User;
import com.elink.enginerring.user.entity.UserRole;
import com.elink.enginerring.user.service.IUserService;
import com.elink.enginerring.util.JSONUtil;

@Controller
@RequestMapping("view/user")
@Api(value="管理员登录接口")
public class UserController {
	@Autowired
	private IUserService userService;
	
	/**
	 * 登录,不拦截
	 * @param phone
	 * @param pwd
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ApiOperation(value="管理员登录", httpMethod = "POST", notes = "管理员通过手机号码和密码登录")
	@ResponseBody
	public Map<String, Object> login(@ApiParam(value="手机号码",required=true)@RequestParam(value="phone",required=true)String phone,
			@ApiParam(value="密码",required=true)@RequestParam(value="pwd",required=true)String pwd,HttpSession session) {
		DataSourceContextHolder.setDbType(DataSourceType.DBUSER);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		User user=userService.getUserByNameAndPwd(phone,pwd);
		//根据用户ID获取用户的角色
		
		if (user!=null) {			
			List<UserRole> roleList = userService.selectRole(user.getUserId());
			session.setAttribute(Constant.su_in_session,user);
			session.setAttribute(Constant.role_in_session,roleList);
			return JSONUtil.getJSON(ErrorType.SUCCESS);
		}
		return JSONUtil.getJSON(ErrorType.SERVER_ERROR);
	}
	
	/**
	 * 修改密码
	 * @param oldpw
	 * @param newpw
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/modifypw", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> modifypw(String oldpw, String newpw,	HttpSession session) {
		//获得session中保存的用户对象
		User user = (User) session.getAttribute(Constant.su_in_session);
		//比较在页面上获得的oldpw与session中的密码是否为同一个用户
		oldpw=DigestUtils.md5DigestAsHex(oldpw.getBytes());
		if (oldpw.equals(user.getPasswd())) {
			String userId = user.getUserId();
			//
			DataSourceContextHolder.setDbType(DataSourceType.DBUSER);
			System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
			
			userService.updateSysUserPw(userId, DigestUtils.md5DigestAsHex(newpw.getBytes()));
			return JSONUtil.getJSON(ErrorType.SUCCESS);
		}else{
			return JSONUtil.getJSON(ErrorType.PASSWORD_ERROR);
		}
	}
	
	/**
	 * 退出
	 * @param session  
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpSession session){
		session.invalidate();
		return "redirect:/login.jsp";
	}
}